package com.wolfword.common.util.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 时间格式化
 * @author ServerZhang
 * @date 2016年4月6日
 */
public class DateUtil {
	
	 /**
	 * 时间格式化（不带秒钟数）
	 * @param date
	 * @return
	 * @author ServerZhang
	 * @date 2016年4月6日
	 */
	public static String getDateWithoutSeconds(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat(
				DateType.YYYY_MM_DD_HH_mm);
		return formatter.format(date);
	}
	/**
	 * 时间格式化（带秒钟数）
	 * @param date
	 * @return
	 * @author ServerZhang
	 * @date 2016年4月6日
	 */
	public static String getDateWithSeconds(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat(
				DateType.YYYY_MM_DD_HH_mm_ss);
		return formatter.format(date);
	}
	 /**
	 * 时间处理为字符串
	 * @param date
	 * @param dateType DateType静态属性
	 * @return
	 * @author ServerZhang
	 * @date 2016年4月18日
	 */
	public static String getDateStr(Date date,String dateType) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat(
				dateType);
		return formatter.format(date);
	}

	/**
	 * 字符串处理为时间
	 * @param date
	 * @return
	 * @author ServerZhang
	 * @date 2016年4月6日
	 */
	public static Date toDate(String o) {
		try {
			DateFormat format = new SimpleDateFormat(DateType.YYYY_MM_DD);
			return format.parse(o);
		} catch (Exception ex) {
			return null;
		}
	}
	/**
	 * 从初始时间到结束时间的所有月份区间
	 * @param startDate
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月23日
	 */
	public static List<Map<String, Object>> getMonthPeriod(Date startDate,Date endDate){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Calendar instance = Calendar.getInstance();
		
		instance.setTime(startDate);
		instance.get(Calendar.YEAR);
		int year = instance.get(Calendar.YEAR);
		int month = instance.get(Calendar.MONTH)+1;
		
		instance.setTime(endDate);
		int endYear = instance.get(Calendar.YEAR);
		int endMonth = instance.get(Calendar.MONTH)+1;
		for(int i=endYear; i>=endYear; i--){
			int start = 12;
			int end = 1;
			if (i == year) {
				end = month;
			}
			if (i == endYear) {
				start = endMonth;
			}
			for (int j = start; j >= end; j--) {
				String tempJ = j > 10 ? j+"" : "0"+j;
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("periodText", i+"年"+tempJ+"月");
				map.put("periodValue", i+"-"+tempJ);
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 根据日期计算星期几
	 * @param date
	 * @return
	 * @author ServerZhang
	 * @date 2017年12月4日
	 */
	public static int dayInWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int index = cal.get(Calendar.DAY_OF_WEEK);
		if (index == 1) {
			return 7;
		}
		return index-1;
	}
	
	/**
	 * 计算两个日期相差几天
	 * @param date1
	 * @param date2
	 * @return
	 * @author ServerZhang
	 * @date 2017年12月4日
	 */
	public static int daysBetween(Date date1,Date date2){
		long time1 = date1.getTime();
		long time2 = date2.getTime();
		int day = 0;
		if (time1 > time2) {
			day = (int)((time1-time2)/(24*60*60*1000));
		}else if (time1 < time2) {
			day = (int)((time2-time1)/(24*60*60*1000));
		}else {
		}
		return day;
	}
	
	
	public static void main(String[] args){
		Calendar instance = Calendar.getInstance();
		instance.set(Calendar.YEAR, 2016);
		System.out.println(daysBetween(instance.getTime(), new Date()));
		/*System.out.println(instance.getTime());
		instance.set(Calendar.DAY_OF_MONTH, 5);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 6);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 7);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 8);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 9);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 10);
		System.out.println(dayInWeek(instance.getTime()));
		instance.set(Calendar.DAY_OF_MONTH, 11);
		System.out.println(dayInWeek(instance.getTime()));*/
	}
	
}
